package group32.client;

import group32.shared.SearchResult;
import group32.shared.UpdateInfo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.rpc.AsyncCallback;
public class HomePage extends Composite implements ValueChangeHandler<String>{
	
	VerticalPanel hp = new VerticalPanel();
	VerticalPanel searchPanel = new VerticalPanel();
	HorizontalPanel mainPanel= new HorizontalPanel();
	VerticalPanel LeftPanel = new VerticalPanel();
	VerticalPanel friendRec = new VerticalPanel();
	VerticalPanel friendBrowse = new VerticalPanel();
	VerticalPanel RightPanel = new VerticalPanel();
	VerticalPanel photoPanel = new VerticalPanel();
	VerticalPanel updatePanel = new VerticalPanel();

	HorizontalPanel search= new HorizontalPanel();
	HorizontalPanel h1= new HorizontalPanel();
		VerticalPanel v1 = new VerticalPanel();
		VerticalPanel v2 = new VerticalPanel();
		HorizontalPanel h2= new HorizontalPanel();
	TextBox searchField = new TextBox();
	Button bSearch= new Button();
	Hyperlink logOff = new Hyperlink();
	Hyperlink friends = new Hyperlink();
	Hyperlink circles = new Hyperlink();
	Hyperlink photos = new Hyperlink();
	Hyperlink page = new Hyperlink();
	VerticalPanel sp= new VerticalPanel();
	VerticalPanel sr= new VerticalPanel();
	static PopupPanel dialogBox;
	final static Button closeButton = new Button("Close");
	static VerticalPanel dialogVPanel;
	public static int uID=0;static String usertype="";
	String login="";
	int FeedSize=20;
	Hyperlink userpage = new Hyperlink();
	Anchor more = new Anchor();
	Anchor more1= new Anchor();
	private HomePageServiceAsync pService = GWT
	.create(HomePageService.class);
	
	public HomePage(final int userID, final String username){
		System.out.println("Homepage userID:"+userID);
		System.out.println("Homepage username:"+username);
		History.addValueChangeHandler(this);
		History.newItem("HomePage");
		uID=userID;
		login=username;
		

		userpage.setText("Welcome "+username);
		userpage.setTitle("Your Profile");
		userpage.setTargetHistoryToken("myprofile");
		
	
		
			hp.setHeight("700px");
		//hp.setVerticalAlignment(VerticalPanel.ALIGN_BOTTOM);

			searchPanel.setVerticalAlignment(HorizontalPanel.ALIGN_BOTTOM);
			searchPanel.setHeight("100px");
			searchPanel.setWidth("1050px");
			bSearch.setText("Search");
			logOff.setText("LogOff");
			logOff.setTargetHistoryToken("logout");
			//v1.setHorizontalAlignment(VerticalPanel.ALIGN_LEFT);
			//v2.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
			v1.add(userpage);
			v2.add(logOff);
			h1.setSpacing(10);
			h2.setSpacing(2);
			h2.add(v1);h2.add(v2);
			circles.setText("Circles");
			circles.setTargetHistoryToken("circles");
			friends.setText("Friends");
			friends.setTargetHistoryToken("friends");
			photos.setText("Photos");
			photos.setVisible(false);
			photos.setTargetHistoryToken("photos");
			h1.add(circles);h1.add(friends);h1.add(photos);
			searchField.setWidth("700px");
			search.setSpacing(10);
			search.add(searchField);search.add(bSearch);
			h1.add(search);
			h1.setVerticalAlignment(HorizontalPanel.ALIGN_BOTTOM);
			h1.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
			searchPanel.add(h2);
			searchPanel.add(h1);
			searchPanel.setStyleName(".paddedHorizontalbottom");
			hp.add(searchPanel);
		
		LeftPanel.setHeight("550px");
		LeftPanel.setWidth("550px");
		LeftPanel.setVerticalAlignment(VerticalPanel.ALIGN_BOTTOM);
		LeftPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
			photoPanel.setWidth("550px");
			photoPanel.setHeight("225px");
			photoPanel.add(new Photo(userID, username, 3));
			updatePanel.setWidth("550px");
			updatePanel.setHeight("225px");
		RightPanel.setHeight("550px");
		RightPanel.setWidth("500px");
		friendRec.setHeight("100px");
		friendRec.setWidth("350px");
		friendRec.add(new Friend(userID,username));
		friendBrowse.setWidth("500px");
		friendBrowse.setHeight("450px");
		//DOM.setInnerHTML(friendBrowse.getElement(), "<div id=\"infovis\"></div> ");
		friendBrowse.add(new FriendBrowser(userID,usertype));
		RightPanel.add(friendRec);
		RightPanel.add(friendBrowse);
		
		mainPanel.add(LeftPanel);
		LeftPanel.add(photoPanel);
		LeftPanel.add(updatePanel);
		updatePanel.add(new HTML("<div id='updateheader'><h2>Updates<h2></div>"));
		updatePanel.add(sp);
		mainPanel.add(RightPanel);
		
		hp.add(mainPanel);
		
		hp.setVerticalAlignment(VerticalPanel.ALIGN_BOTTOM);
		initWidget(hp);
		
		pService.updates(userID,new AsyncCallback<List<UpdateInfo>>() {
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}
			public void onSuccess(List<UpdateInfo> result) {
				if(result!=null)updateFeed(result);
			}
			});
		pService.getUserType(userID,new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}
			public void onSuccess(String result) { 
				if(result!=null)usertype=result;
				else System.err.print("user type return null");
			}
			});

		bSearch.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	        	String keys= searchField.getValue();
	        	if(keys.trim().length()>1)
	        	 FlowControl.go(new Search(keys,userID)); 
	        }
	    });
		closeButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				dialogVPanel.clear();
			}		
		});
		
	}
	
	public void onValueChange(@SuppressWarnings("rawtypes") ValueChangeEvent event){
		System.out.println(uID);
		if(History.getToken().equals("myprofile"))
			 FlowControl.go(new UserProfile(uID,usertype, 0,Login.username,1));             
		if(History.getToken().equals("circles"))
            FlowControl.go(new Circle(uID,login));             
		if(History.getToken().equals("photos"))
            FlowControl.go(new Photo(uID,login,0));   
			if(History.getToken().equals("logout"))
	            FlowControl.go(new Login());             
	}
	
	@SuppressWarnings("deprecation")
	public void updateFeed(List<UpdateInfo>updates){
		if (updates!=null){ int y=0;
		for(UpdateInfo ui : updates){
			String s="";
			if(ui.type.equals("Tag")){
s=ui.firstName+" "+ui.lastName+" tagged picture "+ui.photoID +" with tag "+ui.tags+ "at time " + (Date)ui.timestamp;
			
			}
			else if(ui.type.equals("Upload")){
s=ui.firstName+" "+ui.lastName+" added a new picture "+ui.photoID + " at time " + (Date)ui.timestamp;	
			}
			else System.out.println("unknown update");
			VerticalPanel hp = new VerticalPanel();
			Label l = new Label(s);
			Hyperlink h1 = new Hyperlink();
			h1.setHTML("<img src='" +ui.url+"' style='max-height:75px; max-width:75px'/>");
			final int a = ui.photoID;
			h1.addClickHandler(new ClickHandler() {
		        public void onClick(ClickEvent event) {
			           FlowControl.go(new Photo(uID, a,Login.username,1));             
			        }           
			    });
			hp.add(l);
			hp.add(h1);
			sp.add(hp);	
			y++;
			if(y>=FeedSize){
			more.setHTML("<h5>more..</h5>");
			sp.add(more);
			final int y1=y;
			final List<UpdateInfo> dummy= (List<UpdateInfo>) updates;
			more.addClickHandler(new ClickHandler() {
		        public void onClick(ClickEvent event) {
		           showmore(y1,dummy) ;           
		        }           
		    });
			break;}
		}
	}
	}
	@SuppressWarnings("deprecation")
	public void showmore(int y, List<UpdateInfo> updates){
		sr.remove(more);
		if (updates!=null){ 
		UpdateInfo[] ua = (UpdateInfo[])updates.toArray();
		for(int j=y; j<y+FeedSize;j++){
			UpdateInfo ui = ua[j];
			String s="";
			if(ui.type.equals("Tag")){
s=ui.firstName+" "+ui.lastName+" tagged picture "+ui.photoID +" with tag "+ui.tags+ "at time " + (Date)ui.timestamp;
			
			}
			else if(ui.type.equals("Upload")){
s=ui.firstName+" "+ui.lastName+" added a new picture "+ui.photoID + " at time " + (Date)ui.timestamp;	
			}
			else System.out.println("unknown update");
			VerticalPanel hp = new VerticalPanel();
			Label l = new Label(s);
			Hyperlink h1 = new Hyperlink();
			h1.setHTML("<img src='" +ui.url+"' style='max-height:75px; max-width:75px'/>");
			final int a = ui.photoID;
			h1.addClickHandler(new ClickHandler() {
		        public void onClick(ClickEvent event) {
			           FlowControl.go(new Photo(uID, a,Login.username,1));             
			        }           
			    });
			hp.add(l);
			hp.add(h1);
			sp.add(hp);	
			y++;
			if(y>FeedSize){
			more1.setHTML("<h5>more..</h5>");
			sp.add(more1);
			final  int y1= y;
			final List<UpdateInfo> dummy= (List<UpdateInfo>) updates;
			more1.addClickHandler(new ClickHandler() {
		        public void onClick(ClickEvent event) {
		           showmore((y1+FeedSize),dummy) ;           
		        }           
		    });
			break;}
		}
	}
	}

	public static void showProfile(int uid2, String type,String login) {
		// TODO Auto-generated method stub
		dialogBox = new PopupPanel(false);
		dialogBox.setTitle("User Profile Page");
		dialogBox.setStyleName("demo-PopUpPanel");
		dialogBox.setAnimationEnabled(true);		
		dialogVPanel = new VerticalPanel();
		VerticalPanel content = new VerticalPanel();
		UserProfile.friendID=uID;
		content.add(new UserProfile(uid2,type, uID, login,2));
		dialogVPanel.setVerticalAlignment(VerticalPanel.ALIGN_MIDDLE);
		dialogVPanel.add(content);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);
		dialogBox.show();
		dialogBox.center();
	}
	
}
